/**
 * Dify API测试工具
 * 用于测试与Dify服务的连接
 */

import alovaInstance from '@/api/core/alova.js'
import config from './config.js'

/**
 * 测试Dify API连接
 * @returns {Promise} 测试结果
 */
const testDifyConnection = async () => {
  try {
    // 发送一个简单的测试请求
    const method = alovaInstance.Get(
      `${config.difyConfig.API_URL}/ping`,
      {
        headers: {
          'Authorization': `Bearer ${config.difyConfig.API_KEY}`,
          'Content-Type': 'application/json'
        }
      }
    )
    
    const response = await method.send()
    
    console.log('Dify API连接测试成功:', response)
    return { success: true, data: response }
  } catch (error) {
    console.error('Dify API连接测试失败:', error)
    return { success: false, error: error.message }
  }
}

/**
 * 发送聊天消息到Dify API
 * @param {string} message 用户消息
 * @param {string} conversationId 对话ID（可选）
 * @returns {Promise} API响应
 */
const sendChatMessage = async (message, conversationId = null) => {
  try {
    const payload = {
      inputs: {},
      query: message,
      response_mode: "blocking",
      conversation_id: conversationId,
      user: "gallery_user_test"
    }

    const method = alovaInstance.Post(
      `${config.difyConfig.API_URL}/chat-messages`,
      payload,
      {
        headers: {
          'Authorization': `Bearer ${config.difyConfig.API_KEY}`,
          'Content-Type': 'application/json'
        }
      }
    )
    
    const response = await method.send()
    
    console.log('Dify聊天消息发送成功:', response)
    return { success: true, data: response }
  } catch (error) {
    console.error('Dify聊天消息发送失败:', error)
    return { success: false, error: error.message }
  }
}

export {
  testDifyConnection,
  sendChatMessage
}